<template>
  <div class="about">
    <h1 style="margin-bottom: 1rem">{{ id ? "编辑" : "新建" }}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs model-value="basic" type="border-card">
        <el-tab-pane label="基本信息" name="basic">
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>

          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>

          <el-form-item label="头像">
            <el-upload
              v-model="model.avatar"
              class="avatar-uploader"
              action="http://localhost:3000/admin/api/upload"
              :show-file-list="false"
              :headers="headers"
              :on-success="(res) => (model.avatar = res.url)"
              :before-upload="beforeUpload"
            >
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item label="Banner">
            <el-upload
              v-model="model.banner"
              class="avatar-uploader"
              :show-file-list="false"
              action="http://localhost:3000/admin/api/upload"
              :headers="headers"
              :on-success="(res) => (model.banner = res.url)"
              :before-upload="beforeUpload"
            >
              <img v-if="model.banner" :src="model.banner" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>

          <el-form-item label="类型">
            <el-select
              v-model="model.categories"
              multiple
              placeholder="请选择"
              class="m-2"
              size="large"
            >
              <el-option
                v-for="item of categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              />
            </el-select>
          </el-form-item>

          <!-- show-score 显示 -->
          <el-form-item label="难度">
            <el-rate
              style="margin-top: 0.5rem"
              :max="9"
              v-model="model.scores.difficult"
            ></el-rate>
          </el-form-item>

          <el-form-item label="技能">
            <el-rate
              style="margin-top: 0.5rem"
              :max="9"
              v-model="model.scores.skills"
            ></el-rate>
          </el-form-item>

          <el-form-item label="攻击">
            <el-rate
              style="margin-top: 0.5rem"
              :max="9"
              v-model="model.scores.attack"
            ></el-rate>
          </el-form-item>

          <el-form-item label="生存">
            <el-rate
              style="margin-top: 0.5rem"
              :max="9"
              v-model="model.scores.survive"
            ></el-rate>
          </el-form-item>

          <el-form-item label="顺风出装">
            <el-select
              v-model="model.items1"
              multiple
              placeholder="请选择"
              class="m-2"
              size="large"
            >
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="逆风出装">
            <el-select
              v-model="model.items2"
              multiple
              placeholder="请选择"
              class="m-2"
              size="large"
            >
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="使用技巧">
            <el-input v-model="model.usageTips"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input v-model="model.battleTips"></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input v-model="model.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能" name="skills">
          <el-button @click="model.skills.push({})"
            ><el-icon><Plus /></el-icon> 添加技能</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(item, i) in model.skills" :key="i">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="头像">
                <el-upload
                  v-model="item.icon"
                  class="avatar-uploader"
                  :show-file-list="false"
                  action="http://localhost:3000/admin/api/upload"
                  :headers="headers"
                  :on-success="(res) => (item.icon = res.url)"
                  :before-upload="beforeUpload"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <el-icon v-else class="avatar-uploader-icon"
                    ><Plus
                  /></el-icon>
                </el-upload>
              </el-form-item>
              <el-form-item label="冷却值">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.skills.splice(i, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="最佳搭档" name="partens">
          <el-button @click="model.partens.push({})"
            ><el-icon><Plus /></el-icon> 添加英雄</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(item, i) in model.partens" :key="i">
              <el-form-item label="英雄">
                <el-select filterable v-model="item.hero">
                  <el-option
                    v-for="hero in heroes"
                    :key="hero._id"
                    :value="hero._id"
                    :label="hero.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.partens.splice(i, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-form-item style="margin-top: 1rem">
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { ref, onBeforeMount } from "vue";
import { useRouter } from "vue-router";
import { postNewCategory, getDetail, putDetail, getList } from "@/api";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
export default {
  props: {
    id: {},
  },
  components: {
    Plus,
  },
  setup(props) {
    var model = ref({ scores: {}, skills: [], partens: [] });
    const router = useRouter();
    let categories = ref([]);
    let items = ref([]);
    let heroes = ref([]);
    onBeforeMount(() => {
      props.id && fetch();
      getLists("categories", categories);
      getLists("items", items);
      getLists("heroes", heroes);
    });

    async function save() {
      props.id
        ? await putDetail("heroes", props.id, model)
        : await postNewCategory("heroes", model);
      // console.log(res);
      router.push("/heroes/list");
      ElMessage({
        message: "保存成功",
        type: "success",
      });
    }
    async function fetch() {
      const res = await getDetail("heroes", props.id);
      // model.value = res.data;
      model.value = Object.assign({}, model.value, res.data);
    }
    async function getLists(str, goods) {
      const res = await getList(str);
      goods.value = res.data;
    }
    const headers = {
      Authorization: `Bearer ${localStorage.token}`,
    };

    function beforeUpload(rawFile) {
      // if (rawFile.type !== "image/png") {
      //   ElMessage.error("Avatar picture must be png format!");
      //   return false;
      // } else
      if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error("Avatar picture size can not exceed 2MB!");
        return false;
      }
      return true;
    }
    return { model, save, beforeUpload, categories, items, headers, heroes };
  },
};
</script>
